package tech.aistar.Conteoller;

import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.model.dto.AllJobCompanyDTO;
import tech.aistar.model.dto.Code;
import tech.aistar.model.dto.Result;
import tech.aistar.model.entity.Company;
import tech.aistar.model.entity.Customer;
import tech.aistar.model.entity.Job;
import tech.aistar.service.ICompanyService;
import tech.aistar.service.IJobService;

import java.util.List;

@Controller
@RequestMapping("post")
public class PostsController {

    @Autowired
    private IJobService iJobService;

    @Autowired
    private ICompanyService iCompanyService;

    @Autowired
    private AllJobCompanyDTO allJobCompanyDTO;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping()
    public String posts(HttpServletRequest request){
        if(redisTemplate.hasKey("customer")){
            Customer customer= (Customer) redisTemplate.opsForValue().get("customer");
            request.setAttribute("customer",customer);
        }
        return "posts/Posts";
    }

    @GetMapping("post1")
    public String posts1(HttpServletRequest request){
        return "posts/Posts1";
    }


    @ResponseBody
    @GetMapping("postmsg")
    public Result postMsg(String pageNum){
        Integer pageN= Integer.valueOf(pageNum);
        PageInfo<Job>  pageInfo= iJobService.findAllJob(pageN);
        List<Job> jobs=pageInfo.getList();
        List<Company> companys=iCompanyService.findAllCompany();
        allJobCompanyDTO.setCompanyList(companys);
        allJobCompanyDTO.setJobList(jobs);
        allJobCompanyDTO.setJobPageInfo(pageInfo);
       return new Result(Code.OK,"查询成功",allJobCompanyDTO);
    }

    @ResponseBody
    @GetMapping("postcitysearch")
    public Result postSearch(String pageNum,String city){
        Integer pageN= Integer.valueOf(pageNum);
        PageInfo<Job>  pageInfo=iJobService.findJobByCity(pageN,city);
        List<Job> jobs=pageInfo.getList();
        List<Company> companys=iCompanyService.findAllCompany();
        allJobCompanyDTO.setCompanyList(companys);
        allJobCompanyDTO.setJobList(jobs);
        allJobCompanyDTO.setJobPageInfo(pageInfo);
        return new Result(Code.OK,"查询成功",allJobCompanyDTO);
    }

    @ResponseBody
    @GetMapping("postschoolsearch")
    public Result postSearchBySchool(String pageNum,String school){
        Integer schoolLevel= Integer.valueOf(school);
        Integer pageN= Integer.valueOf(pageNum);
        PageInfo<Job>  pageInfo=iJobService.findJobBySchool(pageN,schoolLevel);
        List<Job> jobs=pageInfo.getList();
        List<Company> companys=iCompanyService.findAllCompany();
        allJobCompanyDTO.setCompanyList(companys);
        allJobCompanyDTO.setJobList(jobs);
        allJobCompanyDTO.setJobPageInfo(pageInfo);
        return new Result(Code.OK,"查询成功",allJobCompanyDTO);
    }

    @ResponseBody
    @GetMapping("postminsearchsearch")
    public Result postSearchByMinMOney(String pageNum,String money){
        Integer minMoney= Integer.valueOf(money);
        Integer pageN= Integer.valueOf(pageNum);
        PageInfo<Job>  pageInfo=iJobService.findJobByMinMoney(pageN,minMoney);
        List<Job> jobs=pageInfo.getList();
        List<Company> companys=iCompanyService.findAllCompany();
        allJobCompanyDTO.setCompanyList(companys);
        allJobCompanyDTO.setJobList(jobs);
        allJobCompanyDTO.setJobPageInfo(pageInfo);
        return new Result(Code.OK,"查询成功",allJobCompanyDTO);
    }

    @ResponseBody
    @GetMapping("postbuttonsearch")
    public Result postSearchByButton(String pageNum,String name){
        Integer pageN= Integer.valueOf(pageNum);
        PageInfo<Job>  pageInfo=iJobService.findJobByButton(pageN,name);
        List<Job> jobs=pageInfo.getList();
        List<Company> companys=iCompanyService.findAllCompany();
        allJobCompanyDTO.setCompanyList(companys);
        allJobCompanyDTO.setJobList(jobs);
        allJobCompanyDTO.setJobPageInfo(pageInfo);
        return new Result(Code.OK,"查询成功",allJobCompanyDTO);
    }
}
